package com.scala.spark.untypedOperation

import org.apache.spark.sql.SparkSession

object WordCount {
    def main(args: Array[String]): Unit = {
         val sparkSession = SparkSession.builder().master("local").appName("wordCount").getOrCreate()
        import sparkSession.implicits._
        val words = sparkSession.sparkContext.textFile("test")
        val flatWord = words.flatMap(x=>x.split(" "))
        val wordAndCount = flatWord.map(Tuple2(_,1))
        val reduceWord = wordAndCount.reduceByKey(_+_)
        val result = reduceWord.map(tuple=>Tuple2(tuple._2,tuple._1)).sortByKey(false).map(tuple=>Tuple2(tuple._2,tuple._1))
        result.foreach(println(_))
        result.collect()

    }
}
